package com.tsmti.webservice.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @date  2018/5/15.
 * @author huanhuan.ying
 */
public class JsonMethod {

    /**
     *
     * @param object 参数
     * @param methodUrl 方法地址
     * @return
     */
    public static String postMethodClient(JSONObject object, String methodUrl) {
        try {
            String args = object.toJSONString();
            HttpClient httpClient = new HttpClient();
            // 设置连接超时时间(单位毫秒)
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(500);
            // 设置读数据超时时间(单位毫秒)
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(2000);

            httpClient.getHostConfiguration().setHost(methodUrl);
            PostMethod method = new PostMethod(methodUrl);
            RequestEntity requestEntity = new StringRequestEntity(object.toString(), "application/json", "UTF-8");
            method.setRequestEntity(requestEntity);
            method.releaseConnection();
            httpClient.executeMethod(method);
            InputStream inputStream = method.getResponseBodyAsStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
            StringBuffer stringBuffer = new StringBuffer();
            String tempbf = bufferedReader.readLine();
            if(!"".equals(ConvertUtil.null2String(tempbf))){
                stringBuffer.append(tempbf);
            }
//            while (tempbf!=null){
//
//            }
            return stringBuffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * get方式获取接口数据
     * @param interfaceUrl  rest接口地址
     * @param methodUrl  包含参数的UTL地址
     * @return
     */
    public static String getMethodClient(String interfaceUrl,String methodUrl) {
        GetMethod method = null;
        try {
            HttpClient httpClient = new HttpClient();
            // 设置连接超时时间(单位毫秒)
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(500);
            // 设置读数据超时时间(单位毫秒)
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(2000);

            httpClient.getHostConfiguration().setHost(interfaceUrl);
            method = new GetMethod(methodUrl);
            method.releaseConnection();
            httpClient.executeMethod(method);
            InputStream inputStream = method.getResponseBodyAsStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            while (!"".equals(ConvertUtil.null2String(bufferedReader.readLine()))){
                stringBuffer.append(bufferedReader.readLine());
            }
            return stringBuffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
